package a10_动态规划;

/**
 * <p>
 * a48_不同的子序列
 * </p>
 *
 * @author flyduck
 * @since 2025/2/28
 */
public class a48_不同的子序列 {
    //s中有多少种删除元素的方式

    //dp[i][j]:以i-1为结尾的chars1中包含以j-1结尾的chars2中 的个数 为dp[i][j]

    //递推公式：
    //s=bagg
    //t= bag
    // if(chars1[i-1] == chars2[j-1]){
    //     dp[i][j] = dp[i-1][j-1] + dp[i-1][j];
    // }else{
    //     dp[i][j] = dp[i-1][j];
    // }

    //初始化
    //dp[i][0] = 1
    //dp[0][j] = 0
    //dp[0][0] = 1
    public int numDistinct(String s, String t) {
        char[] chars1 = s.toCharArray();
        char[] chars2 = t.toCharArray();

        int[][] dp = new int[chars1.length+1][chars2.length+1];
        dp[0][0] = 1;
        for (int i = 1; i <= chars1.length; i++) {
            dp[i][0] = 1;
        }

        for (int j = 1; j <= chars2.length; j++) {
            dp[0][j] = 0;
        }

        for (int i = 1; i <= chars1.length; i++) {
            for (int j = 1; j <= chars2.length; j++) {
                if(chars1[i-1] == chars2[j-1]){
                    dp[i][j] = dp[i-1][j-1] + dp[i-1][j];
                }else{
                    dp[i][j] = dp[i-1][j];
                }
            }
        }

        return dp[chars1.length][chars2.length];
    }
}
